import './style/app.less'
import  './utils/ui-toast'
import  './utils/ajax'
import  {getUrlParam} from './utils/base'
import MiniRefreshTools from 'minirefresh';

(function () {
    //下拉刷新
    var miniRefresh = new MiniRefresh({
        container: '#minirefresh',
        down: {
            callback: function() {
                // 下拉事件
                init()
                miniRefresh.endDownLoading(true);
            }
        },
        up: {

            callback: function() {
                // 上拉事件

                // 注意，由于默认情况是开启满屏自动加载的，所以请求失败时，请务必endUpLoading(true)，防止无限请求
                miniRefresh.endUpLoading(true);
            },
            contentnomore:''
        }
    });

    let deviceId='';
     if(getUrlParam('deviceId')){
         deviceId=getUrlParam('deviceId')
     }

     //初始化数据
    function init(){
        getJSON(`/ricebucket/api/v1/query?deviceId=${deviceId}`, function(result) {
            if(result.head.returnCode==0){
                if(result.head.subCode==0){
                    if(result.body){
                        let riceBucketId=result.body.riceBucketId ||'',
                            temperature=result.body.temperature ||'',
                            humidity=result.body.humidity ||'',
                            rssi=result.body.rssi ||'',
                            power=result.body.power ||'',
                            weight=result.body.weight || 0,
                            updateTime=result.body.updateTime || '' ;


                        document.querySelector('.riceBucketId').innerHTML= riceBucketId
                        document.querySelector('.temperature').innerHTML= temperature+'℃'
                        document.querySelector('.humidity').innerHTML= humidity+'%RH'
                        document.querySelector('.rssi').innerHTML= rssi
                        document.querySelector('.power').innerHTML= power
                        document.querySelector('.updateTime').innerHTML= updateTime
                        document.querySelector('.weight').innerHTML= weight
                    }else {
                        toast('暂无信息')
                    }

                }else {
                    toast(result.head.subMsg)
                }
            }else {
                toast(result.head.returnMsg)
            }
        });
    }

    init()

})()




